#include "ball.h"



Ball::Ball()
{
   x=ORIGINAL_BALL_X;
   y=ORIGINAL_BALL_Y;
   direct = ORIGINAL_DIRECT;
   has_shot = 0;

   ball = new QRect(ORIGINAL_BALL_X,ORIGINAL_BALL_Y,BALL_RADIUS*2,BALL_RADIUS*2);


}

Ball::~Ball(){delete ball;}


void Ball::inite_ball()
{
    x=ORIGINAL_BALL_X;
    y=ORIGINAL_BALL_Y;
    direct = ORIGINAL_DIRECT;
    has_shot = 0;
    ball->setX(x);
    ball->setY(y);

}

bool Ball::m_intersects(int xx,int yy)
{
    if(x+BALL_RADIUS*2>=xx-5&&x+BALL_RADIUS*2<=xx+5&&y>=yy-5&&y<=yy+5)//球左板右
    {
        return true;
    }
    if(x<=xx+BRICK_LENGTH+5&&x>=xx+BRICK_WIDTH-5&&y>=yy-5&&y<=yy+5)//球右板左
    {
        return true;
    }
    if(y>=yy+BRICK_WIDTH-5&&y<=yy+BRICK_WIDTH+5&&x>=xx-5&&x<=xx+BRICK_LENGTH+5)//球下板上
    {
        return true;
    }
    if(y>=yy-BRICK_WIDTH-5&&y<=yy-BRICK_WIDTH+5&&x>=xx-5&&x<=xx+BRICK_LENGTH+5)//球上板下
    {
        return true;
    }

    return false;

}

void Ball::move()
{
    if(has_shot==false)
      {return;}
    x=x+5*cos((direct*PI)/180);
    y=y-5*sin((direct*PI)/180);
    ball->setX(x);
    ball->setY(y);
}

void Ball::moveto(int i)
{
    x = i;
}



void Ball::switch_direct_brick(int xx,int yy)//把砖的坐标传进来
{
  if(has_shot==0)
      {return;}

  if(y>=yy+BRICK_WIDTH-5&&y<=yy+BRICK_WIDTH+5&&x+BALL_RADIUS*2>=xx-5&&x<=xx+BRICK_LENGTH+5)
      {//球下板上
       direct = -direct;
       return;
      }

  if(y>=yy-BRICK_WIDTH-5&&y<=yy-BRICK_WIDTH+5&&x+BALL_RADIUS*2>=xx-5&&x<=xx+BRICK_LENGTH+5)
      {//球上板下
        direct = -direct;
        return;
      }

  if(x<=xx+BRICK_LENGTH+5&&x>=xx+BRICK_WIDTH-5&&y>=yy-5&&y<=yy+5)
      {//球右板左
       if(direct >= 0 )
       {
           direct = 180-direct;
              return;
       }
       else
       {
           direct = -(180+direct);
              return;
       }
      }

  if(x+BALL_RADIUS*2>=xx-5&&x+BALL_RADIUS*2<=xx+5&&y>=yy-5&&y<=yy+5)
   {//球左板右
       if(direct >= 0 )
       {
           direct = 180-direct;
           return;
       }
       else
       {
           direct = -(180+direct);
           return;
       }
   }


      direct = -direct;
      return;
}



void Ball::switch_direct3(QRect *player)
{

        if(has_shot==0)
        {
            return;
        }

        if(y<=830&&y>=800-BALL_RADIUS*2&&x==player->x()+BOARD_LENGTH/2-BALL_RADIUS)
          {
            direct = 60;
          }
        if(y<=830&&y>=800-BALL_RADIUS*2&&x>=player->x()&&x<player->x()+BOARD_LENGTH/2-BALL_RADIUS)
        {
            direct = 120;

        }
        if(y<=830&&y>=800-BALL_RADIUS*2&&x>player->x()+BOARD_LENGTH/2-BALL_RADIUS&&x<=player->x()+BOARD_LENGTH)
        {
            direct = 60;
        }

        if(y<=830&&y>=800-BALL_RADIUS*2&&x<=player->x()&&x>=player->x()-5)
        {
            if(direct >= 0 )
            {
                direct = 180-direct;
            }
            else
            {
                direct = -(180+direct);
            }
        }
        if(y<=830&&y>=800-BALL_RADIUS*2&&x>=player->x()+BOARD_LENGTH&&x<=player->x()+BOARD_LENGTH+5)
        {
            if(direct >= 0 )
            {
                direct = 180-direct;
            }
            else
            {
                direct = -(180+direct);
            }
        }

}

